package cn.dt.ymcc.web.controller;

import cn.dt.ymcc.domain.PayOrder;
import cn.dt.ymcc.dto.AlipayNotifyDto;
import cn.dt.ymcc.dto.PayApplyDto;
import cn.dt.ymcc.result.JSONResult;
import cn.dt.ymcc.service.IAlipayInfoService;
import cn.dt.ymcc.service.IPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("pay")
public class PayController {
    @Autowired
    private IPayOrderService payOrderService;


    /**
     * 根据订单号校验支付单是否已经存在
     * @param orderNo
     * @return
     */
    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo){
        PayOrder payOrder = payOrderService.selectByOrderNo(orderNo);
        // 支付单已经存在那么就返回success  支付单没有存在就返回false
        return payOrder == null? JSONResult.error() : JSONResult.success();
    }
    /**
     * 发起支付申请
     * @param dto
     * @return
     */
    @PostMapping("/apply")
    public JSONResult apply(@RequestBody PayApplyDto dto){
        String formStr= payOrderService.apply(dto);// 向支付宝发起申请  返回from表单
        return JSONResult.success(formStr);
    }

    /**
     * 支付成功之后的回调，这个接口是从t_alipay_info的notify_url字段来的
     * @param orderNo
     * @return
     */
    @RequestMapping("/alipay/notify")
    public String notify(AlipayNotifyDto dto) {
        try {
            return payOrderService.successCallback(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
    }

}
